class CouriersController < ApplicationController
  before_filter :except => [:current_stops, :active_checklist, :update] do |controller|
    controller.authorize_permissions({:blocked_permissions => (User::PermissionLevels - ["admin"])})
  end

  # GET /couriers
  # GET /couriers.json
  def index
    @title = "View Couriers"
    @couriers = Courier.all

    respond_to do |format|
      format.html # index.html.erb
      format.json  { render :json => @couriers }
    end
  end
  
  def active_checklist
    @courer = Courer.find(params["id"])
    # TODO: Get the real active checklist
    @checklist = @courier.checklist[0]
    respond_to do |format|
      format.json  { render :json => @courier }
    end
  end

  # GET /couriers/1
  # GET /couriers/1.json
  def show
    @courier = Courier.find(params[:id])
    @title = "View Courier"

    respond_to do |format|
      format.html # show.html.erb
      format.json  { render :json => @courier }
    end
  end

  # GET /couriers/new
  # GET /couriers/new.json
  def new
    @title = "New Courier"
    @courier = Courier.new

    respond_to do |format|
      format.html # new.html.erb
      format.json  { render :json => @courier }
    end
  end

  # GET /couriers/1/edit
  def edit
	@courier = Courier.find(params[:id])
    @title = "Edit Courier: " + @courier.user.real_name
  end

  # POST /couriers
  # POST /couriers.json
  def create
    @courier = Courier.new(params[:courier])

    respond_to do |format|
      if @courier.save
        flash[:notice] = 'Courier was successfully created.'
        format.html { redirect_to(@courier) }
        format.json  { render :json => @courier, :status => :created, :location => @courier }
      else
		@title = "New Courier"
        format.html { render :action => "new" }
        format.json  { render :json => @courier.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /couriers/1
  # PUT /couriers/1.json
  def update
    @courier = Courier.find(params[:id])

    respond_to do |format|
      if @courier.update_attributes(params[:courier])
        flash[:notice] = 'Courier was successfully updated.'
        format.html { redirect_to(@courier) }
        format.json  { render :json => @courier }
      else
		@title = "Edit Courier: " + @courier.user.real_name
        format.html { render :action => "edit" }
        format.json  { render :json => @courier.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /couriers/1
  # DELETE /couriers/1.json
  def destroy
    @courier = Courier.find(params[:id])
    @courier.destroy

    respond_to do |format|
      format.html { redirect_to(couriers_url) }
      format.json  { head :ok }
    end
  end
  
  def make_all_not_picked_up
    Item.all.each do |x|
        x.picked_up = false
        x.save
    end
    respond_to do |format|
      format.html { redirect_to(couriers_url) }
      format.json  { head :ok }
    end
  end
  
  def current_stops
    courier = Courier.find(params[:id])
		# Get all routes for the current courier
    instances = RouteInstance.find_all_by_courier_id(courier.id,
      :conditions => ["day = ?", Date.today]) || []
    route_id_dates = instances.map {|x| [x.route_id, x.day]}
    
    rules = courier.route_recurrence_rules.map {|x|
      x.route.future_route_instances_for(courier.id, 1).first
    }.reject(&:nil?).select {|x|
      # hack, hack, hack, hack, hack
      x.day == Date.today and not route_id_dates.member? [x.route_id, x.day]
    }

    @stops = (instances + rules).map {|x| x.stop_instances || []}.flatten
		
    respond_to do |format|
        format.json  { render :json => @stops }
    end
  end
  
end
